//
//  DDRTGroupListViewController.m
//  DDRichTextDemo
//
//  Created by 张小伟 on 2021/9/19.
//

#import "DDRTGroupListViewController.h"
#import "DDRTHeader.h"
#import "DDRTSelectedManager.h"
#import "DDRTSelectedMemberTableCell.h"
#import "DDRTTextBtnViewController.h"
#import "DDRTGroupMemberModel.h"


@interface DDRTGroupListViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIButton *rightNavBtn;

@property(nonatomic,strong)UITableView *tableView;

// tableView的所有数据源的管理类（包括搜索和选择的情况）
@property(nonatomic,strong)DDRTSelectedManager *selectedGroupManager;

@end

@implementation DDRTGroupListViewController

#pragma mark - Init

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat rightNavBtnW = 50;
    CGFloat subViewH = 30;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightNavBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.rightNavBtn.frame = CGRectMake(0, 0, rightNavBtnW, subViewH);
    self.navigationItem.title = @"成员列表";

    
    CGFloat tableViewY = kNavBarHeight;
    self.tableView.frame = CGRectMake(0, tableViewY, kScreenWidth, kScreenHeight - tableViewY);
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.rightNavBtn];

    
    // 把上次的单例对象清空
    self.selectedGroupManager = nil;
    [self.selectedGroupManager initAllDatas];
    [self.tableView reloadData];
    
    [self.selectedGroupManager addObserver:self forKeyPath:@"selectedArr" options:NSKeyValueObservingOptionNew context:nil];
    
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.selectedGroupManager.selectedArr.count == 0) {
        self.rightNavBtn.selected = NO;
    }
    [self.tableView reloadData];
}

-(void)dealloc {
    
    [self.selectedGroupManager removeObserver:self forKeyPath:@"selectedArr"];
}

#pragma mark - Private Methods


#pragma mark - Action Methods
-(void)rightNavBtnClick:(UIButton *)btn {
    if (btn.selected == YES) { // 群聊功能
        DDLog(@"self.selectedGroupManager.selectedArr = %@",self.selectedGroupManager.selectedArr);
        if (self.selectedGroupManager.selectedArr.count > 0) {
            DDRTTextBtnViewController *testBtnVC = [[DDRTTextBtnViewController alloc] init];
            [self.navigationController pushViewController:testBtnVC animated:YES];
        }
       
    }else { // 退出当前页面功能
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
//    DDLog(@"change = %@",change);
    if (self.selectedGroupManager.selectedArr.count != 0) {
        self.rightNavBtn.selected = YES;
    }else {
        self.rightNavBtn.selected = NO;
    }
}


#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.selectedGroupManager.dataArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row < self.selectedGroupManager.dataArr.count) {
        DDRTGroupMemberModel *model = self.selectedGroupManager.dataArr[indexPath.row];
        DDRTSelectedMemberTableCell *searchCell = [DDRTSelectedMemberTableCell updateWithTableView:tableView];
        searchCell.groupMemberModel = model;
        return searchCell;
        
    }
    
    return nil;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return kCWInviteFriendsTableCellHeight;
    if (indexPath.row < self.selectedGroupManager.dataArr.count) {
        return kDDRTSelectedMemberTableCellHeight;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.selectedGroupManager.dataArr.count) {
        self.selectedGroupManager.dataArr = [self.selectedGroupManager updateItemManager:indexPath];
        [self.tableView reloadData];
        
        self.selectedGroupManager.selectedArr = [self.selectedGroupManager getAllSelectedDatas];
//        CWInviteFriendsModel *itemModel = self.selectedGroupManager.dataArr[indexPath.row];
        
    }
}


#pragma mark - Setter & Getter

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}


-(UIButton *)rightNavBtn {
    if (!_rightNavBtn) {
        _rightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightNavBtn.titleLabel.font =[UIFont systemFontOfSize:18];
        [_rightNavBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_rightNavBtn setTitle:@"完成" forState:UIControlStateSelected];
        [_rightNavBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightNavBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
        [_rightNavBtn addTarget:self action:@selector(rightNavBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightNavBtn;
}

-(DDRTSelectedManager *)selectedGroupManager {
    if (!_selectedGroupManager) {
        _selectedGroupManager = [DDRTSelectedManager shareSelectedGroupManagerInstance];
    }
    return _selectedGroupManager;
}


@end
